package com.wzh.mysending.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wzh.mysending.R;
import com.wzh.mysending.base.BaseActivity;
import com.wzh.mysending.config.Constant;
import com.wzh.mysending.service.KeepService;
import com.wzh.mysending.service.MainService;

import com.wzutils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_switch)
    CheckBox mySwitch;
    @BindView(R.id.queit_layout)
    LinearLayout queitLayout;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.ver_no)
    TextView verNo;
    Intent intent;
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    put(Constant.ISOPEN, isChecked);
                    intent.setClass(context, MainService.class);
                    if (isChecked) {
                        startService(intent);
                    } else {
                        stopService(intent);
                    }


                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "设置");
        intent = getIntent();
        bundle = intent.getExtras();
        mySwitch.setChecked((Boolean) get(Constant.ISOPEN, false));


    }

    private void showWarron() {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setIcon(R.mipmap.logo);
        normalDialog.setTitle("提示：确定要退出吗？");
        normalDialog.setMessage("退出后无法再上传定位");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        intent.setClass(context,MainService.class);
                        stopService(intent);
                        intent.setClass(context,KeepService.class);
                        stopService(intent);
                        SPUtils.clear(context);
                        MainActivity.instance.finish();
                        intent.setClass(context, LonginActivity.class);
                        startActivity(intent);
                        finish();

                    }
                });
        normalDialog.setNegativeButton("算了",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();

    }

    @OnClick(R.id.queit_layout)
    public void onClick() {
       showWarron();

    }
}
